<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function median(...nums) {
            return nums.sort()[Math.floor(nums.length/2)]
        }

        const proxy = new Proxy(median, {
            apply(target, thisArg, argumentsList) {
                for (const arg of argumentsList) {
                    if (typeof arg !== 'number') {
                        throw 'Non-number argunment provided'
                    }
                }
                return Reflect.apply(...arguments)
            }
        })
        console.log(proxy(4,7,1)) // 4
        console.log(proxy(1,'2',3)) // 报错

        // 实例化时给参数传值
        class User {
            constructor(id) {
                this.id = id
            }
        }
        const proxy1 = new Proxy(User, {
            construct(target, argumentsList, newTarget) {
                if(argumentsList[0] === undefined) {
                    throw 'User cannot be id'
                } else {
                    return Reflect.construct(...arguments)
                }
            }
        })
        new User(1)
        new User() // 报错
    </script>
</body>
</html>